package com.xl.utils.util;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author xionglin
 * @Classname DateUitl
 * @Description TODO
 * @Date 2019/5/30 17:46
 */
@Slf4j
public class DateUitl {

    /**
     * 私有化构成器避免工具类被实例化
     */
    private DateUitl(){};


    public static String dataToStr(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(date == null){
            return "";
        }
        return sdf.format(date);
    }

    /**
     * date如果为空返回空串，否则返回带有时间格式的字符串
     * @param date 时间
     * @param pattern 时间格式（yyyy-MM-dd）
     * @return
     */
    public static String dataToStr(Date date,String pattern) throws Exception{
            if(dateIsNull(date)){
                return "";
            }
            if(ObjIsEmpty(pattern)){
                //pattern为空按yyyy-MM-dd格式处理
                return dataToStr(date);
            }
            return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * Obj(Obj只能是String Array List Map这四个类型)如果为null返回false；如果是空字符串返回false；如果是空的数组，Map,List返回false，否则返回true
     * @param obj
     * @return
     */
    public static Boolean ObjIsNotEmpty(Object obj) throws Exception{
        if(obj == null){
            return false;
        }else{
            if(obj instanceof String){
                return obj.toString().length()>0;
            }else if(obj.getClass().isArray()){
                Object[] array=  (Object[])obj;
                return array.length>0;
            }else if(obj instanceof List){
                List list = (List)obj;
                return !list.isEmpty();
            }else if(obj instanceof Map){
                Map map = (Map)obj;
                return !map.isEmpty();
            }else{
                //不是String Array List Map 直接返回false
                log.info("ObjIsEmpty方法的参数只能为String Array List Map这四个类型");
            throw new Exception("ObjIsEmpty方法的参数只能为String Array List Map这四个类型");
            }
        }
    }

    public static Boolean ObjIsEmpty(Object obj) throws Exception{
        return !ObjIsNotEmpty(obj);
    }

    public static Boolean dateIsNull(Date date){
        if(date == null){
            return true;
        }
        return false;
    }

    public static Boolean dateIsNotNull(Date date){
       return !dateIsNull(date);
    }

    /**
     * @Description: 判断是否有参数为空
     * @Param: Object的参数数组
     * @Author: xionglin @Date: 2019/5/11 11:23
     * @return: true :判断没有空 false :判断有空 @Author: xionglin @Date: 2019/5/11 11:23
     */
    public static boolean paramCheck(Object[] object) {
        if (object == null) {
            return false;
        }
        for (Object obj : object) {
            if (obj == null) {
                return false;
            }
            if (obj instanceof String) {
                if ("".equals(obj.toString())) {
                    return false;
                }
            }
        }
        return true;
    }
    public static Map<String,Object> converGbkToUtf8(Map<String,Object> map){
        Map<String, Object> result = Collections.synchronizedMap(new HashMap<>());
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        try {
            for(Map.Entry entry:entries){
                String key = (String)entry.getKey();
                Object value = entry.getValue();
                String key_ = new String(key.getBytes("gbk"), "UTF-8");
                String value_ = new String(value.toString().getBytes("gbk"), "UTF-8");
                result.put(key_,value_);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
     /*   HashMap<String, Object> map = new HashMap<>();
        map.put("哈哈","as");
        try {
            converGbkToUtf8(map);
        } catch (Exception e) {
            e.printStackTrace();
        }*//*
     String str = "杞︽柟閲?";
        try {
            String encode = URLEncoder.encode(str, "utf-8");
            URLDecoder.decode()

            URLEncoder.
            String s= new String(encode.getBytes("gbk"), "UTF-8");
            System.out.println(s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/


        String s = "CSS测试";
        try {
            //css or js post/get data submmit
            s = URLEncoder.encode(s, "UTF-8");
            System.out.println("encode :"+s);
            //backstage java/jsp dispose
            s = URLDecoder.decode(s, "UTF-8");
            System.out.println("decode :"+s);
            s = new String(s.getBytes(),"GBK");
            System.out.println("GBK :" +s);
            //combine encoding change
            //	s = new String(URLDecoder.decode(s, "UTF-8").getBytes(),"GBK");
        } catch (UnsupportedEncodingException e) {
            System.out.println("encoding cause,change failure");
        }catch (Exception e) {
            System.out.println("others cause,change failure");
        }
    }
}
